package serializable;

import java.io.*;
import java.util.ArrayList;

public class SerializableUDemo {
    public static void main(String[] args) throws IOException {
        // 序列化/反序列化User对象
        serializeUser();

        // ArrayList的序列化
        serializeableArrayList();

        // 序列化存储规则
        serializeStorageRule();
    }

    private static void serializeUser() {

        // Initializes The Object

        User user = new User("xiao ming", 18, "male");

        System.out.println(user); // User{name='xiao ming', age=18, gender='male'}

        File file = new File("C:\\Temp\\test\\user.txt");

        // Write Object to File

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
            oos.writeObject(user);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 静态变量
        User.from = "sdf";

        // Read Object from File

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            User newUser = (User) ois.readObject();
            System.out.println(newUser);  // User{name='xiao ming', age=18, gender='null'}
        } catch (ClassNotFoundException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void serializeableArrayList() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");

        System.out.println(list.toString());

        File file = new File("C:\\Temp\\test\\user.txt");

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
             ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        ) {
            oos.writeObject(list);
            oos.flush();

            ArrayList<String> newList = (ArrayList<String>) ois.readObject();
            System.out.println(newList);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }



    }

    private static void serializeStorageRule() {
        User user = new User("xiao ming", 18, "male");
        File file = new File("C:\\Temp\\test\\user");

        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
             ObjectInputStream oin = new ObjectInputStream(new FileInputStream(file));
        ) {
            out.writeObject(user);
            out.flush();

            System.out.println(file.length()); // 88
            user.setName("mingming");

            out.writeObject(user);
            out.close();
            System.out.println(file.length()); // 93

            User t1 = (User) oin.readObject();
            User t2 = (User) oin.readObject();

            System.out.println(t1 == t2); // true
            System.out.println(t1.getName()); // xiao ming
            System.out.println(t2.getName()); // xiao ming

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
